
import {Http, Response} from "@angular/http";
import {Component} from "@angular/core";


@Component({
  selector:'catch-err',
  template:`<button (click)="errClick($event)">捕捉错误</button>
  <p>{{requestUrl}}</p>`
})
export class CatchErrorComponent{
  http: Http;
  requestUrl: string ;
  constructor(http:Http){
    this.http = http;
  }
  public errClick(event){
    this.requestUrl = "http://www.baidu.com"
    this.http.request(this.requestUrl).subscribe(//订阅
      (res:Response)=>{
        let result = res.json(); alert(result)},
      (error)=> {
        alert(error)// 错误捕捉,回调
        this.requestUrl = "";
      },
          ()=>
            alert("完成")//complete，():void方法，可选
    );

  }

}
